package com.ddwl.goods.service.bargain;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.bargain.BargainActivityDao;
import com.ddwl.goods.dao.bargain.BargainOrderDao;
import com.ddwl.goods.dao.bargain.BargainSkuDao;
import com.ddwl.goods.model.bargain.BargainActivity;
import com.ddwl.goods.model.bargain.BargainOrder;
import com.ddwl.goods.model.bargain.BargainSku;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.bargain.BargainOrderPageBo;
import com.ddwl.schema.bo.goods.bargain.MyBargainOrderPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.bargain.BargainOrderDetailVo;
import com.ddwl.schema.vo.goods.bargain.BargainOrderVo;
import com.ddwl.schema.vo.goods.bargain.BargainSkuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hemiaojun
 * @Date 2020/10/26 9:45
 */
@Service
public class BargainOrderService {

    @Resource
    private BargainOrderDao bargainOrderDao;
    @Resource
    private BargainSkuDao bargainSkuDao;
    @Resource
    private BargainActivityDao bargainActivityDao;


    public PageVo<BargainOrderVo> page(PageBo<BargainOrderPageBo> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        PageInfo<BargainOrder> info = new PageInfo<>(bargainOrderDao.getList(bo.getParam()));
        List<BargainOrderVo> list = BeanConverUtils.convertBeanList(info.getList(), BargainOrderVo.class);
        return new PageVo<>(info.getTotal(), bo.getPage(), bo.getPageSize(), list);
    }

    public BargainOrderVo getById(String bargainOrderId) {
        return bargainOrderDao.getDetailById(bargainOrderId);
    }

    public void update(BargainOrder bargainOrder) {
        bargainOrderDao.updateByPrimaryKeySelective(bargainOrder);
    }

    public BargainOrderDetailVo detail(String bargainOrderId) {
        BargainOrderVo bargainOrderVo = getById(bargainOrderId);
        if (null == bargainOrderVo || StrUtil.isEmpty(bargainOrderVo.getItemId())) {
            return null;
        }
        BargainOrderDetailVo bodVo = BeanConverUtils.convertBean(bargainOrderVo, BargainOrderDetailVo.class);
        Boolean processing = GoodsEnum.BargainOrderStatusEnum.PROCESSING.getCode().equals(bodVo.getStatus()) && Instant.now().compareTo(bodVo.getExpireDate()) > 0;
        Boolean threshold = GoodsEnum.BargainOrderStatusEnum.THRESHOLD_BUY.getCode().equals(bodVo.getStatus()) && Instant.now().compareTo(bodVo.getPayExpireDate()) > 0;
        if (processing || threshold) {
            bodVo.setStatus(GoodsEnum.BargainOrderStatusEnum.FAILURE.getCode());
        }
        BargainSkuVo bsVo = bargainSkuDao.getBargainSkuVo(bargainOrderVo.getBarcode());
        bodVo.setBargainSkuVo(bsVo);
        return bodVo;
    }

    public PageVo<BargainOrderVo> myList(PageBo<MyBargainOrderPageBo> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        PageInfo<BargainOrderVo> info = new PageInfo<>(bargainOrderDao.myList(bo.getParam()));
        return new PageVo<>(info.getTotal(), bo.getPage(), bo.getPageSize(), info.getList());
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeExpiredStatus() {
        List<BargainActivity> bargainActivityList = bargainActivityDao.findExpiredActivity();
        if (!CollectionUtils.isEmpty(bargainActivityList)) {
            List<String> ids = bargainActivityList.stream().map(BargainActivity::getId).collect(Collectors.toList());
            bargainActivityDao.batchUpdateStatus(ids);
        }
        List<BargainOrder> list = bargainOrderDao.findInvalidOrder();
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<BargainActivity> baList = new ArrayList<>();
        List<BargainSku> bsList = new ArrayList<>();
        List<String> idList = new ArrayList<>();
        list.forEach(bo -> {
            idList.add(bo.getId());
            BargainActivity bargainActivity = baList.stream().filter(ba -> bo.getActivityId().equals(ba.getId())).findFirst().orElse(null);
            if (null == bargainActivity) {
                bargainActivity = new BargainActivity();
                bargainActivity.setId(bo.getActivityId());
                bargainActivity.setSurplusStock(1);
                baList.add(bargainActivity);
            } else {
                bargainActivity.setSurplusStock(bargainActivity.getSurplusStock() + 1);
            }

            BargainSku bargainSku = bsList.stream()
                    .filter(bs -> bo.getActivityId().equals(bs.getActivityId()) && bo.getBarcode().equals(bs.getBarcode()))
                    .findFirst().orElse(null);
            if (null == bargainSku) {
                bargainSku = new BargainSku();
                bargainSku.setActivityId(bo.getActivityId());
                bargainSku.setBarcode(bo.getBarcode());
                bargainSku.setSales(1);
                bargainSku.setChopStock(1);
                bsList.add(bargainSku);
            } else {
                bargainSku.setSales(bargainSku.getSales() + 1);
                bargainSku.setChopStock(bargainSku.getChopStock() + 1);
            }
        });
        bargainOrderDao.batchUpdateStatus(idList);
        bargainActivityDao.batchUpdateStock(baList);
        bargainSkuDao.batchUpdateSales(bsList);
    }
}
